// pages/route/bike.js
const amap = require('../../utils/amap-config')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listHeight: "50px",
        listShowed: false,
    },
    // 跳转页面
    changeNav: function (e) {
        // console.log(e)
        wx.navigateTo({
          url: '/pages/route/'+e.currentTarget.dataset.type+'?poilocation='+this.data.poilocation,
        })
    },
    // 查看导航文本
    checkDetail: function () {
        if(this.data.listShowed){
            this.setData({
                listHeight: "50px",
                listShowed: false, 
            }) 
        } else {
            this.setData({
                listHeight: "80vh",
                listShowed: true, 
            })   
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options)
        const that = this
        if (options.poilocation !== ''){
            // 设置/修改数据
            that.setData({
                poilocation: options.poilocation,
                markers: [
                    {
                        id: 0,
                        longitude: wx.getStorageSync('longitude'),
                        latitude: wx.getStorageSync('latitude'),
                        iconPath:'../../static/images/nav_s.png',
                        width: 40,
                        height: 40,
                    },
                    {
                        id: 1,
                        longitude: parseFloat(options.poilocation.split(',')[0]),
                        latitude: parseFloat(options.poilocation.split(',')[1]),
                        iconPath:'../../static/images/nav_e.png',
                        width: 40,
                        height: 40,
                    }
                ]
            })
            // 当前的位置经纬度    
            const origin = wx.getStorageSync('longitude')+ "," +wx.getStorageSync('latitude')
            // console.log(origin)
            // console.log(options.poilocation)
            // 调用步行导航的接口
            amap.map.getRidingRoute({
                origin: origin,
                destination: options.poilocation,
                success: function(res) {
                    // console.log(res)
                    let points = []
                    if (res.paths && res.paths[0] && res.paths[0].steps){
                        var steps = res.paths[0].steps
                        for (let i = 0; i<steps.length;i++){
                            var polylineData = steps[i].polyline.split(';')
                            for (let j = 0; j<polylineData.length;j++){
                               points.push({
                                   longitude: parseFloat(polylineData[j].split(',')[0]),
                                   latitude: parseFloat(polylineData[j].split(',')[1])
                               })
                            }
                        }
                    }
                    that.setData({
                        polyline:[{
                            points: points,
                            color: "#0091ff",
                            width: 6
                        }],
                        steps: res.paths[0].steps
                    })

                    if  (res.paths[0] && res.paths[0].distance){
                        that.setData({
                            distance: parseFloat(res.paths[0].distance / 1000).toFixed(2) + '公里'
                        })
                    }
                    if  (res.paths[0] && res.paths[0].duration){
                        that.setData({
                            duration: parseFloat(res.paths[0].duration / 60).toFixed(2) + '分钟'
                        })
                    }
                }
            })
        } 
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})